package com.game;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import java.awt.CardLayout;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Toolkit;
import java.awt.GridLayout;
import java.awt.Color;
import java.util.HashMap;
import java.util.Map;

import javax.swing.border.BevelBorder;
import javax.swing.border.TitledBorder;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.border.SoftBevelBorder;

@SuppressWarnings("serial")
public class MAIN_APP extends JFrame {
	JLabel lblMonmey = new JLabel("Memory:");
	private JPanel contentPane;
	boolean []cards=new boolean[16];
	JLabel []cradslab=new JLabel[16];
	@SuppressWarnings("rawtypes")
	Map Crads=new HashMap();
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MAIN_APP frame = new MAIN_APP();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	@SuppressWarnings({ "unchecked", "static-access" })
	public MAIN_APP() {
  		try{
  			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  		}catch(Exception e){
  		}
		setIconImage(Toolkit.getDefaultToolkit().getImage(MAIN_APP.class.getResource("/com/res/icon48.png")));
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 400, 550);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new CardLayout(0, 0));
		
		for(int i=0;i<16;i++){
			Crads.put(i,"0");
		}
		
		JPanel panel = new JPanel();
		contentPane.add(panel, "name_8033353753439");
		
		JLabel label = new JLabel("1024!");
		label.setFont(new Font("微软雅黑", Font.PLAIN, 94));
		panel.add(label);
		
		JButton btnPlayNow = new JButton("PLAY NOW");
		btnPlayNow.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				CardLayout cl=(CardLayout) contentPane.getLayout();
				cl.next(contentPane);
			}
		});
		btnPlayNow.setFont(new Font("微软雅黑", Font.PLAIN, 22));
		panel.add(btnPlayNow);
		
		JPanel panel_1 = new JPanel();
		contentPane.add(panel_1, "name_8035651674003");
		panel_1.setLayout(null);
		
		JPanel panel_2 = new JPanel();
		panel_2.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
		panel_2.setBackground(Color.WHITE);
		panel_2.setBounds(68, 88, 239, 299);
		panel_1.add(panel_2);
		panel_2.setLayout(new GridLayout(4, 0, 0, 0));
		
		JButton button = new JButton("\u4E0A");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Thread Down=new CDown();
				Down.start();
			}
		});
		button.setBounds(69, 53, 238, 34);
		panel_1.add(button);
		
		JButton button_1 = new JButton("\u4E0B");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Thread Up=new CUp();
				Up.start();
			}
		});
		button_1.setBounds(68, 387, 239, 50);
		panel_1.add(button_1);
		
		JButton button_2 = new JButton("\u5DE6");
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Thread Left=new CLeft();
				Left.start();
			}
		});
		button_2.setBounds(10, 88, 57, 299);
		panel_1.add(button_2);
		
		JButton button_3 = new JButton("\u53F3");
		button_3.setBounds(310, 88, 57, 299);
		panel_1.add(button_3);
		
		lblMonmey.setFont(new Font("宋体", Font.PLAIN, 12));
		lblMonmey.setBounds(0, 496, 187, 15);
		panel_1.add(lblMonmey);
		
		for(int i=0;i<16;i++){
			cradslab[i]=new JLabel();
			cradslab[i].setText(" ");
			cradslab[i].setFont(new Font("微软雅黑", Font.PLAIN, 38));
			panel_2.add(cradslab[i]);
		}
		int i=0;
		while(i<16){
			Cards.a[i]=false;
			i++;
		}
		Cards crad = new Cards();
		crad.create();
		Crads.put(crad.c1, "2");
		Crads.put(crad.c2,"2");
		Crads.put(crad.c3, "2");
		System.out.print(Crads.get(crad.c1)+","+Crads.get(crad.c1)+","+Crads.get(crad.c1));
		cradslab[crad.c1].setText("2");
		cradslab[crad.c2].setText("2");
		cradslab[crad.c3].setText("2");
		Thread MW=new MemoryW();
		MW.start();
	}
	class MemoryW extends Thread{
		public void run(){
			while(true){
				try {
					Thread.sleep(990);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				lblMonmey.setText(null);
				try {
					Thread.sleep(10);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			lblMonmey.setText("Memory:"+((Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory())/275296)+"MB");
			}
		}
	}
	class CDown extends Thread{
		@SuppressWarnings("unchecked")
		public void run(){
		for(int i=0;i<16;i++){
			if(Crads.get(i-4)!=null&&Integer.parseInt((String) Crads.get(i-4))==0){
				cradslab[i-4].setText((String) Crads.get(i));
				if(Integer.parseInt((String) Crads.get(i))==0){
					cradslab[i-4].setText(" ");
				}
				Crads.put(i-4, Crads.get(i));
				Crads.put(i, "0");
				cradslab[i].setText(" ");
			}
		}
		for(int i=0;i<16;i++){
			if(Crads.get(i-4)!=null&&Integer.parseInt((String) Crads.get(i-4))==0){
				cradslab[i-4].setText((String) Crads.get(i));
				if(Integer.parseInt((String) Crads.get(i))==0){
					cradslab[i-4].setText(" ");
				}
				Crads.put(i-4, Crads.get(i));
				Crads.put(i, "0");
				cradslab[i].setText(" ");
			}
		}
		for(int i=0;i<16;i++){
			if(Crads.get(i-4)!=null&&Integer.parseInt((String) Crads.get(i-4))==0){
				cradslab[i-4].setText((String) Crads.get(i));
				if(Integer.parseInt((String) Crads.get(i))==0){
					cradslab[i-4].setText(" ");
				}
				Crads.put(i-4, Crads.get(i));
				Crads.put(i, "0");
				cradslab[i].setText(" ");
			}
		}
		for(int i=0;i<16;i++){
			if(Crads.get(i-4)!=null&&Integer.parseInt((String) Crads.get(i-4))==0){
				cradslab[i-4].setText((String) Crads.get(i));
				if(Integer.parseInt((String) Crads.get(i))==0){
					cradslab[i-4].setText(" ");
				}
				Crads.put(i-4, Crads.get(i));
				Crads.put(i, "0");
				cradslab[i].setText(" ");
			}
		}
		for(int i=0;i<16;i++){
			if(Crads.get(i-4)!=null&&Integer.parseInt((String) Crads.get(i-4))==Integer.parseInt((String) Crads.get(i))){
				cradslab[i-4].setText(Integer.parseInt((String) Crads.get(i))*2+"");
				if(Integer.parseInt((String) Crads.get(i))==0){
					cradslab[i-4].setText(" ");
				}
				Crads.put(i-4, Integer.parseInt((String) Crads.get(i))*2+"");
				Crads.put(i, "0");
				cradslab[i].setText(" ");
			}
		}
		Cards crad = new Cards();
		crad.addCards();
		for(int i=0;i<16;i++){
		if(Integer.parseInt((String) Crads.get(i))!=0||crad.value!=i){
//		if(Integer.parseInt((String) Crads.get(i))!=0){
		}else if(crad.value==i){
		Crads.put(crad.value, "2");
		cradslab[crad.value].setText("2");
		break;
		}
		}
		}
	}
	class CUp extends Thread{
		@SuppressWarnings("unchecked")
		public void run(){
		for(int i=0;i<16;i++){
			if(Crads.get(i+4)!=null&&Integer.parseInt((String) Crads.get(i+4))==0){
				cradslab[i+4].setText((String) Crads.get(i));
				if(Integer.parseInt((String) Crads.get(i))==0){
					cradslab[i+4].setText(" ");
				}
				Crads.put(i+4, Crads.get(i));
				Crads.put(i, "0");
				cradslab[i].setText(" ");
			}
		}
		for(int i=0;i<16;i++){
			if(Crads.get(i+4)!=null&&Integer.parseInt((String) Crads.get(i+4))==0){
				cradslab[i+4].setText((String) Crads.get(i));
				if(Integer.parseInt((String) Crads.get(i))==0){
					cradslab[i+4].setText(" ");
				}
				Crads.put(i+4, Crads.get(i));
				Crads.put(i, "0");
				cradslab[i].setText(" ");
			}
		}
		for(int i=0;i<16;i++){
			if(Crads.get(i+4)!=null&&Integer.parseInt((String) Crads.get(i+4))==0){
				cradslab[i+4].setText((String) Crads.get(i));
				if(Integer.parseInt((String) Crads.get(i))==0){
					cradslab[i+4].setText(" ");
				}
				Crads.put(i+4, Crads.get(i));
				Crads.put(i, "0");
				cradslab[i].setText(" ");
			}
		}
		for(int i=0;i<16;i++){
			if(Crads.get(i+4)!=null&&Integer.parseInt((String) Crads.get(i+4))==0){
				cradslab[i+4].setText((String) Crads.get(i));
				if(Integer.parseInt((String) Crads.get(i))==0){
					cradslab[i+4].setText(" ");
				}
				Crads.put(i+4, Crads.get(i));
				Crads.put(i, "0");
				cradslab[i].setText(" ");
			}
		}
		for(int i=0;i<16;i++){
			if(Crads.get(i+4)!=null&&Integer.parseInt((String) Crads.get(i+4))==Integer.parseInt((String) Crads.get(i))){
				cradslab[i+4].setText(Integer.parseInt((String) Crads.get(i))*2+"");
				if(Integer.parseInt((String) Crads.get(i))==0){
					cradslab[i+4].setText(" ");
				}
				Crads.put(i+4, Integer.parseInt((String) Crads.get(i))*2+"");
				Crads.put(i, "0");
				cradslab[i].setText(" ");
			}
		}
		Cards crad = new Cards();
		crad.addCards();
		for(int i=0;i<16;i++){
		if(Integer.parseInt((String) Crads.get(i))!=0||crad.value!=i){
//		if(Integer.parseInt((String) Crads.get(i))!=0){
		}else if(crad.value==i){
		Crads.put(crad.value, "2");
		cradslab[crad.value].setText("2");
		break;
		}
		}
		}
	}
	class CLeft extends Thread{
		@SuppressWarnings("unchecked")
		public void run(){
		for(int i=0;i<16;i++){
			if(i-1!=15&&Crads.get(i-1)!=null&&i-1!=11&&i-1!=7&&i-1!=3&&i-1!=-1&&Integer.parseInt((String) Crads.get(i-1))==0){
				cradslab[i-1].setText((String) Crads.get(i));
				if(Integer.parseInt((String) Crads.get(i))==0){
					cradslab[i-1].setText(" ");
				}
				Crads.put(i-1, Crads.get(i));
				Crads.put(i, "0");
				cradslab[i].setText(" ");
			}
		}
		for(int i=0;i<16;i++){
			if(i-1!=15&&Crads.get(i-1)!=null&&i-1!=11&&i-1!=7&&i-1!=3&&i-1!=-1&&Integer.parseInt((String) Crads.get(i-1))==0){
				cradslab[i-1].setText((String) Crads.get(i));
				if(Integer.parseInt((String) Crads.get(i))==0){
					cradslab[i-1].setText(" ");
				}
				Crads.put(i-1, Crads.get(i));
				Crads.put(i, "0");
				cradslab[i].setText(" ");
			}
		}
		for(int i=0;i<16;i++){
			if(i-1!=15&&Crads.get(i-1)!=null&&i-1!=11&&i-1!=7&&i-1!=3&&i-1!=-1&&Integer.parseInt((String) Crads.get(i-1))==0){
				cradslab[i-1].setText((String) Crads.get(i));
				if(Integer.parseInt((String) Crads.get(i))==0){
					cradslab[i-1].setText(" ");
				}
				Crads.put(i-1, Crads.get(i));
				Crads.put(i, "0");
				cradslab[i].setText(" ");
			}
		}
		for(int i=0;i<16;i++){
			if(i-1!=15&&Crads.get(i-1)!=null&&i-1!=11&&i-1!=7&&i-1!=3&&i-1!=-1&&Integer.parseInt((String) Crads.get(i-1))==0){
				cradslab[i-1].setText((String) Crads.get(i));
				if(Integer.parseInt((String) Crads.get(i))==0){
					cradslab[i-1].setText(" ");
				}
				Crads.put(i-1, Crads.get(i));
				Crads.put(i, "0");
				cradslab[i].setText(" ");
			}
		}
		for(int i=0;i<16;i++){
			if(i-1!=15&&Crads.get(i-1)!=null&&i-1!=11&&i-1!=7&&i-1!=3&&i-1!=-1&&Integer.parseInt((String) Crads.get(i-1))==Integer.parseInt((String) Crads.get(i))){
				cradslab[i-1].setText(Integer.parseInt((String) Crads.get(i))*2+"");
				if(Integer.parseInt((String) Crads.get(i))==0){
					cradslab[i-1].setText(" ");
				}
				Crads.put(i-1, Integer.parseInt((String) Crads.get(i))*2+"");
				Crads.put(i, "0");
				cradslab[i].setText(" ");
			}
		}
		Cards crad = new Cards();
		crad.addCards();
		for(int i=0;i<16;i++){
		if(Integer.parseInt((String) Crads.get(i))!=0||crad.value!=i){
//		if(Integer.parseInt((String) Crads.get(i))!=0){
		}else if(crad.value==i){
		Crads.put(crad.value, "2");
		cradslab[crad.value].setText("2");
		break;
		}
		}
		}
	}
}